package mypazar.denemecartago;

import java.util.Iterator;
import java.util.List;

import cartago.AgentId;
import cartago.ArtifactConfig;
import cartago.ArtifactId;
import cartago.ArtifactObsProperty;
import cartago.CartagoException;
import cartago.CartagoNode;
import cartago.CartagoService;
import cartago.CartagoWorkspace;
import cartago.ICartagoSession;
import cartago.Op;
import cartago.WorkspaceKernel;
import cartago.security.AgentIdCredential;
import cartago.util.agent.Agent;
import cartago.util.agent.CartagoBasicContext;

public class PazarEnvironmentMain {

	private static final String PAZAR_ENV_NAME = "apple";
	public static final String AGENT_NAME_SMAN1 = "salesman1";
	private static final String STALL1_PRODUCT_NAME = "apple";

	private static final String[] STALL1_PRODUCT_NAME_OBJECT = { "apple" };

	private static final String BUYING_PRODUCT_NAME = STALL1_PRODUCT_NAME;
	private static int BUYING_QUANTITY = 5;

	/**
	 * @param args
	 * @throws CartagoException
	 */
	public static void main(String[] args) throws Exception {

		// CartagoService.startNode();
		CartagoNode node = CartagoNode.getInstance();
		CartagoWorkspace workspace = node.createWorkspace(PAZAR_ENV_NAME);

		@SuppressWarnings("unused")
		Agent agent = new Agent(AGENT_NAME_SMAN1);

		System.out.println("++: " + workspace.getId().getName());

		AgentIdCredential agentIdCredential = new AgentIdCredential(
				AGENT_NAME_SMAN1);

		AgentId agentID = new AgentId(AGENT_NAME_SMAN1, "40", 10, "Salesman",
				workspace.getId());

		CartagoListener eventListener = new CartagoBasicContext(
				AGENT_NAME_SMAN1).new CartagoListener();

		ICartagoSession session = CartagoService.startSession(PAZAR_ENV_NAME,
				agentIdCredential, eventListener);

		WorkspaceKernel kernel = workspace.getKernel();

		ArtifactId stallArtifactId = kernel.makeArtifact(agentID, "Stall",
				"mypazar.cartago.Stall",
				new ArtifactConfig(STALL1_PRODUCT_NAME));

		Op opStallHello = new Op("sayHello");

		Op opReturnSmth = new Op("retSmth", "asddeneme");

		long doActStHello = session.doAction(stallArtifactId, opStallHello,
				null, -1);

		long doActReturnSmth = session.doAction(stallArtifactId, opReturnSmth,
				null, -1);

		session.doAction(stallArtifactId, new Op("sell", BUYING_PRODUCT_NAME,
				BUYING_QUANTITY), null, -1);

		System.out.println("doActStHello: " + doActStHello);

		System.out.println("doActReturn: " + doActReturnSmth);

		List<ArtifactObsProperty> focusList = kernel.focus(agentID, null, null,
				stallArtifactId);

		for (Iterator<ArtifactObsProperty> iterator = focusList.iterator(); iterator
				.hasNext();) {
			ArtifactObsProperty artifactObsProperty = (ArtifactObsProperty) iterator
					.next();

			System.out.println("+++++++++++++++++++++++ "
					+ artifactObsProperty.getValue());

		}
		System.out.println("-- " + focusList.get(0));

		CartagoBasicContext cartagoBasicContext = new CartagoBasicContext(
				AGENT_NAME_SMAN1, PAZAR_ENV_NAME);

		cartagoBasicContext.makeArtifact("Stall", "mypazar.cartago.Stall",
				STALL1_PRODUCT_NAME_OBJECT);

	}
}
